<template>
  <Category :scene="scene"/>
  <el-card style="margin: 20px 0">
    <div v-show="scene === 0">
      <el-button
        type="primary"
        icon="Plus"
        :disabled="!categoryStore.c3Id"
        @click="onAddSpu"
      >
        添加SPU
      </el-button>
      <el-table style="margin: 20px 0" border stripe :data="records">
        <el-table-column
          label="序号"
          align="center"
          type="index"
          width="100px"
        ></el-table-column>
        <el-table-column
          label="SPU名称"
          align="center"
          prop="spuName"
        ></el-table-column>
        <el-table-column
          label="SPU描述"
          align="center"
          prop="description"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="操作" align="center" width="300px">
          <template #="{ row, $index }">
            <el-button
              type="primary"
              icon="Plus"
              title="添加SKU"
              @click="onAddSku(row)"
            ></el-button>
            <el-button
              type="warning"
              icon="Edit"
              title="修改SPU"
              @click="onEdit(row)"
            ></el-button>
            <el-button
              type="info"
              icon="View"
              title="查看SKU列表"
              @click="onFindSku(row)"
            ></el-button>
            <el-popconfirm
              :title="`你确定要删除${row.spuName}吗？`"
              width="200px"
              @confirm="onDeleteSku(row)"
            >
              <template #reference>
                <el-button
                  type="danger"
                  icon="Delete"
                  title="删除SPU"
                ></el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!--分页器-->
      <el-pagination
        v-model:current-page="pageNo"
        v-model:page-size="pageSize"
        :background="true"
        :page-sizes="[10, 20, 30, 40]"
        layout=" prev, pager, next, jumper,->,total, sizes"
        :total="total"
        :disabled="!categoryStore.c3Id"
        @size-change="getHasSpu"
        @current-change="getHasSpu"
      />
    </div>
    <!--添加、修改spu子组件-->
    <SpuForm ref="spu" v-show="scene === 1" @changeScene="changeScene"/>
    <!--添加sku的子组件-->
    <SkuForm ref="sku" v-show="scene === 2" @changeScene="changeScene"/>
    <!--dialog对话框:展示已有的SKU数据-->
    <el-dialog title="SKU列表" v-model="show">
      <el-table style="margin: 20px 0" border stripe :data="skuArr">
        <el-table-column
          label="SKU名字"
          prop="skuName"
          align="center"
        ></el-table-column>
        <el-table-column
          label="SKU价格"
          prop="price"
          align="center"
        ></el-table-column>
        <el-table-column
          label="SKU重量"
          prop="weight"
          align="center"
        ></el-table-column>
        <el-table-column label="SKU图片" align="center">
          <template #="{ row, $index }">
            <img :src="row.skuDefaultImg" alt="" style="width: 100px"/>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </el-card>
</template>
<script setup lang="ts">
import type {
  HasSpuResponseData,
  Records,
  SkuData,
  SkuInfoData,
  SpuData,
} from '@/api/product/spu/type.ts'
import {onBeforeUnmount, ref, watch} from 'vue'
// 引入分类的仓库
import useCategoryStore from '@/store/modules/category.ts'
import {reqHasSpu, reqRemoveSpu, reqSkuList} from '@/api/product/spu'
// 引入子组件
import SpuForm from '@/views/product/spu/spuForm.vue'
import SkuForm from '@/views/product/spu/skuForm.vue'
import {ElMessage} from 'element-plus'

let categoryStore = useCategoryStore()

let scene = ref<number>(0) // 0显示已有spu 1添加、修改已有的spu 2添加sku
let pageNo = ref<number>(1)
let pageSize = ref<number>(10)
let total = ref<number>(0)
let records = ref<Records>([])
// 获取子组件实例
let spu = ref<any>()
let sku = ref<any>()
// 存储全部的SKU数据
let skuArr = ref<SkuData[]>([])
let show = ref<boolean>(false)

// 监听三级分类ID变化
watch(
  () => categoryStore.c3Id,
  () => {
    if (!categoryStore.c3Id) return
    getHasSpu()
  },
)
// 此方法执行可以获取某一个三级分类下面全部的SPU
const getHasSpu = async (pager = 1) => {
  pageNo.value = pager
  let result: HasSpuResponseData = await reqHasSpu(
    pageNo.value,
    pageSize.value,
    categoryStore.c3Id,
  )
  if (result.code === 200) {
    records.value = result.data.records
    total.value = result.data.total
  }
}
// 点击添加spu按钮
const onAddSpu = () => {
  scene.value = 1
  spu.value.initAddSpu(categoryStore.c3Id)
}
// 点击修改spu按钮
const onEdit = (row: SpuData) => {
  scene.value = 1
  // 调用子组件实例方法
  spu.value.initHasSpuData(row)
}
// 子组件自定义事件
const changeScene = (obj: any) => {
  scene.value = obj.flag
  if (obj.params === 'update') {
    getHasSpu(pageNo.value)
  } else {
    getHasSpu()
  }
}
// 点击添加SKU按钮
const onAddSku = (row: SpuData) => {
  scene.value = 2
  // 调用子组件的方法初始化添加SKU的数据
  sku.value.initSkuData(categoryStore.c1Id, categoryStore.c2Id, row)
}
// 点击查看SKU
const onFindSku = async (row: SpuData) => {
  let result: SkuInfoData = await reqSkuList(row.id)
  if (result.code === 200) {
    skuArr = result.data
    show.value = true
  }
}
// 点击删除SKU
const onDeleteSku = async (row: SpuData) => {
  let result = await reqRemoveSpu(row.id)
  if (result.code === 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    await getHasSpu(records.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }
}
// 路由组件销毁前，清空仓库关于分类的数据
onBeforeUnmount(() => {
  categoryStore.$reset()
})
</script>
<style scoped lang="scss"></style>
